package com.lyxy.controller;


import com.lyxy.pojo.Dept;
import com.lyxy.pojo.Result;
import com.lyxy.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*部门管理*/


@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    //自动注入
    @Autowired
    private DeptService deptService;
//    private static Logger log= (Logger) LoggerFactory.getLogger(DeptController.class);   等同于@Slf4j

    //    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据");
        List<Dept> alldept = deptService.list();
        return Result.success(alldept);
    }


    //根据ID删除部门数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门数据:{}", id);
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) { //@RequestBody 将前端传过来的json数据封装到dept对象中
        log.info("添加部门数据");
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("使用id:{}查询",id);
        Dept dept=deptService.get(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门数据");
        deptService.modify(dept);
        return Result.success();
    }



}
